<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="author" content="Yb">
    <title>JQ点击加号上传图片</title>
    <script src="../js/jquery-3.1.1.js"></script>
    <style>
        #chooseImg img {
            height: 100px;
            width: 100px;
        }
        
        #chooseImg input {
            display: none;
        }
    </style>
</head>

<body>
    <form action="#">
        <div id="chooseImg">
            <img src="img/add.png" alt="">
            <input type="file">
            <img src="img/add.png" alt="">
            <input type="file">
            <img src="img/add.png" alt="">
            <input type="file">
        </div>
        <div id="upload">
            <button type="button">添加</button>
            <button type="submit">上传</button>
        </div>
    </form>
</body>

</html>
<script>
    function clickFile() {
        $('img').each(function (index) {
            $(this).click(function () {
                $(this).next().click();
            })
        })
    }
    clickFile();
    $(':button').click(function () {
        var img = document.createElement('img');
        img.src = 'img/add.png';
        $('#chooseImg').append(img);
        var input = document.createElement('input');
        input.type = 'file';
        $('#chooseImg').append(input);
        clickFile();
        sh();
    })

    function sh() {
        $('input[type=file]').each(function () {
            $(this).change(function (ev) {
                var file = $(this).prop('files')[0];
                // console.log(file);
                if (file.type.startsWith('image')) {
                    $(this).prev().attr('src', URL.createObjectURL(file))
                } else {
                    alert('只能上传图片');
                }
            })
        })
    }

</script>